//
//  SelectImagePopV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/30.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class SelectImagePopV: UIView {
  
  var takePhotoClosure: VoidClosure?
  var selectClosure: VoidClosure?
  
  fileprivate var backgroundV: UIView!
  fileprivate var contentV: UIView!
  
  fileprivate var takePhotoButton: UIButton!
  fileprivate var selectButton: UIButton!
  fileprivate var cancleButton: UIButton!
  
  fileprivate var separateV: UIView!
  
  fileprivate var fromFrame: CGRect!
  fileprivate var endFrame: CGRect!
  
  override init(frame: CGRect) {
    
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    fromFrame = CGRect(x: 0, y: UIScreen.height + 200, width: UIScreen.width, height: 200)
    endFrame = CGRect(x: 0, y: UIScreen.height - 200, width: UIScreen.width, height: 200)
    
    create()
    gesture()
  }
  
  required init?(coder aDecoder: NSCoder) {
    
    fatalError("init(coder:) has not been implemented")
    
  }
  
}

// MARK: - <#note#>

extension SelectImagePopV {
  
  func show(in view: UIView){
    
    view.addSubview(self)
    UIView.animate(withDuration: 0.48) {
      self.contentV.frame = self.endFrame
      self.backgroundV.alpha = 1
    }
    
  }
  
}


// MARK: - action

extension SelectImagePopV {
  
  @objc fileprivate func takePhotoAction() {
    guard let closure = takePhotoClosure else { return }
    closure()
    dismiss()
  }
  
  @objc fileprivate func selectAction() {
    guard let closure = selectClosure else { return }
    closure()
    dismiss()
  }
  
  @objc fileprivate func dismiss() {
    UIView.animate(withDuration: 0.48, animations: {
      self.contentV.frame = self.fromFrame
      self.backgroundV.alpha = 0
    }) { (true) in
      self.removeFromSuperview()
    }
  }
}

// MARK: - gesture

extension SelectImagePopV {
  
  fileprivate func gesture() {
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
    backgroundV.addGestureRecognizer(tap)
    
  }
  
}

// MARK: - fileprivate

extension SelectImagePopV {
  
  fileprivate func create() {
    
    backgroundV = UIView(frame: UIScreen.main.bounds)
    addSubview(backgroundV)
    backgroundV.backgroundColor = GlobalConfig.BGLightGreyColor
    backgroundV.alpha = 0
    
    contentV = UIView(frame: fromFrame)
    contentV.backgroundColor = UIColor.white
    addSubview(contentV)
    
    takePhotoButton = UIButton()
    contentV.addSubview(takePhotoButton)
    takePhotoButton.backgroundColor = UIColor.white
    takePhotoButton.setTitle("拍照", for: .normal)
    takePhotoButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    takePhotoButton.titleLabel?.font = GlobalConfig.system16Font
    takePhotoButton.addTarget(self, action: #selector(takePhotoAction), for: .touchUpInside)
    takePhotoButton.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(20)
      make.height.equalTo(50)
    }
    
    selectButton = UIButton()
    contentV.addSubview(selectButton)
    selectButton.backgroundColor = UIColor.white
    selectButton.setTitle("从相册选择", for: .normal)
    selectButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    selectButton.titleLabel?.font = GlobalConfig.system16Font
    selectButton.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
    selectButton.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(takePhotoButton.snp.bottom).offset(10)
      make.height.equalTo(50)
    }
    
    separateV = UIView()
    contentV.addSubview(separateV)
    separateV.backgroundColor = GlobalConfig.BGWhiteColor
    separateV.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.height.equalTo(10)
      make.top.equalTo(selectButton.snp.bottom).offset(10)
    }
    
    cancleButton = UIButton()
    contentV.addSubview(cancleButton)
    cancleButton.backgroundColor = UIColor.white
    cancleButton.setTitle("取消", for: .normal)
    cancleButton.setTitleColor(GlobalConfig.TextDeepColor, for: .normal)
    cancleButton.titleLabel?.font = GlobalConfig.system16Font
    cancleButton.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
    cancleButton.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(separateV.snp.bottom)
      make.height.equalTo(50)
    }

  }
}
